package com.neulogistics.contrib.usercenter.domain.model.enumeration;

import com.neulogistics.contrib.usercenter.domain.exception.UserCenterDomainException;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Enumeration;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class UserRole extends Enumeration {

    /**
     * 用户状态: 0：管理员 1:客服中心 2:调度中心 3:中心库房 4: 分站库房 5: 分站 6: 配送中心 7:财务部 8:配送员
     */
    public static final int Administrator_Id = 0;
    public static final int CustomerServiceCenter_Id = 1;
    public static final int TransferCenter_Id = 2;
    public static final int CentralWarehouse_Id = 3;
    public static final int SubWarehouse_Id = 4;
    public static final int Substation_Id = 5;
    public static final int ProcuredCenter_Id = 6;
    public static final int FinanceDepartment_Id = 7;
    public static final int DeliveryPersonnel_Id = 8;

    public static final UserRole ADMINISTRATOR = new UserRole(Administrator_Id, "Administrator");
    public static final UserRole CUSTOMER_SERVICE_CENTER = new UserRole(CustomerServiceCenter_Id, "Customer Service Center");
    public static final UserRole TRANSFER_CENTER = new UserRole(TransferCenter_Id, "Transfer Center");
    public static final UserRole CENTRAL_WAREHOUSE = new UserRole(CentralWarehouse_Id, "Central Warehouse");
    public static final UserRole SUB_WAREHOUSE = new UserRole(SubWarehouse_Id, "Sub Warehouse");
    public static final UserRole SUBSTATION = new UserRole(Substation_Id, "Substation");
    public static final UserRole PROCURED_CENTER = new UserRole(ProcuredCenter_Id, "Procured Center");
    public static final UserRole FINANCE_DEPARTMENT = new UserRole(FinanceDepartment_Id, "Finance Department");
    public static final UserRole DELIVERY_PERSONNEL = new UserRole(DeliveryPersonnel_Id, "Delivery Personnel");

    private static final List<UserRole> list = Arrays.asList(
            ADMINISTRATOR,
            CUSTOMER_SERVICE_CENTER,
            TRANSFER_CENTER,
            CENTRAL_WAREHOUSE,
            SUB_WAREHOUSE,
            SUBSTATION,
            PROCURED_CENTER,
            FINANCE_DEPARTMENT,
            DELIVERY_PERSONNEL
    );

    protected UserRole(int id, String name) {
        super(id, name);
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }

    public static List<UserRole> getList() {
        return list;
    }

    public static UserRole fromName(String name) throws UserCenterDomainException {
        Optional<UserRole> optional = list.stream().filter(p -> p.name.equals(name)).findFirst();

        if (optional.isPresent()) {
            return optional.get();
        } else {
            throw new UserCenterDomainException(
                    ErrorCode.OPERATION_ERROR,
                    String.format("Possible values for {%s}: %s",
                            UserRole.class.getSimpleName(),
                            list.stream()
                                    .map(p -> p.name)
                                    .collect(Collectors.joining(", ")))
            );
        }
    }
}
